/* * @Author: hhy * @Date: 2019-01-30 14:11:45 * @Last Modified by: chenjie *
@Last Modified time: 2019-08-05 10:58:33 * @文件说明: echarts组件 */
<template>
  <div>
    <div class="h-echarts" ref="hEcharts" id="hEcharts" :style="style" />
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  name: "h-echarts",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "500",
    },
    option: {
      type: Object,
      default() {
        return {};
      },
    },
    color: {
      type: String,
      default() {
        return "#7DB6EF";
      },
    },
    Names: {
      type: String,
      default() {
        return "票数";
      },
    },
    type: {
      type: String,
      default() {
        return "";
      },
    },
  },
  watch: {
    type: {
      handler: function (val) {
        this.init();
      },
      deep: true,
    },
  },
  computed: {
    style() {
      return {
        width: this.width.match("%") ? this.width : this.width + "px",
        height: this.height + "px",
      };
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let legendData = this.option.legendData;
      let series = this.option.series;
      let xAxis = this.option.xAxis;

      const option = {
        color: ["#CC99FF", "#FF0000", "#FF9900", "#1890FF", "#00CC00"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        legend: {
          data: legendData,
        },
        grid: {
          left: "0%",
          right: "0%",
          bottom: "0%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: xAxis,
          min: 0,
        },
        yAxis: {
          type: "value",
        },
        series: series,
      };
      // 基于准备好的dom，初始化echarts实例
      const myChart = echarts.init(this.$refs.hEcharts);
      // 绘制图表
      myChart.setOption(option);
    },
  },
};
</script>
